package pab.ui;
import java.awt.*;
import java.awt.event.*;
import java.util.Calendar;
import java.util.Date;

import javax.swing.*;

import pab.core.transaction.Transaction;
/*
 * Created by JFormDesigner on Fri Jun 17 23:45:27 ICT 2011
 */

/**
 * @author rukowen
 */
public class IntervalSelection extends JDialog {
	/**
	 * 
	 */
	
	private static final long serialVersionUID = 1L;
	Transaction trans;
	public boolean isSelectOk;
	public IntervalSelection(Frame owner, Transaction trans) {
		super(owner);
		this.trans = trans;
		isSelectOk = false;
		initComponents();
		loadInterval();
	}

	private void loadInterval(){
		cboInterval.addItem("Month");
		cboInterval.addItem("Quarter");
		cboInterval.addItem("6-Month");
		cboInterval.addItem("Year");
	}
	
	
	public IntervalSelection(Dialog owner) {
		super(owner);
		initComponents();
	}

	private int numMonth(){
		String inteval = cboInterval.getSelectedItem().toString();
		if (inteval.equals("Month"))
			return 1;
		if (inteval.equals("Quarter"))
			return 3;
		if (inteval.equals("6-Month"))
			return 6;
		return 12;
	}
	private void btnOkMouseClicked(MouseEvent e) {
		// TODO add your code here
		//XMLGregorianCalendarReturn 0, means dates are equal
		//Return 1, if first date is greater than second date
		//Return -1, if first date is less than second date
		
		Calendar currentDate = Calendar.getInstance();
		Date transDate = GeneralAuxiliary.XMLGregorianCalendarToDate(trans.getDate());
		currentDate.add(Calendar.MONTH, -numMonth());
		//transdate + interval > curr date: ok
		//transdate > currdate - interval: ok
		if(transDate.compareTo(currentDate.getTime()) == -1 || transDate.compareTo(currentDate.getTime()) == 0){
			JOptionPane.showMessageDialog(this, "Invalid interval ( transaction date + interval period must greater than current date");
		}
		else{
			this.isSelectOk = true;
			this.dispose();
		}
	}

	private void initComponents() {
		// JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
		label1 = new JLabel();
		cboInterval = new JComboBox();
		btnOk = new JButton();

		//======== this ========
		setTitle("Select Interval");
		setModal(true);
		Container contentPane = getContentPane();
		contentPane.setLayout(null);

		//---- label1 ----
		label1.setText("Interval");
		contentPane.add(label1);
		label1.setBounds(5, 15, 50, label1.getPreferredSize().height);
		contentPane.add(cboInterval);
		cboInterval.setBounds(65, 10, 140, cboInterval.getPreferredSize().height);

		//---- btnOk ----
		btnOk.setText("Ok");
		btnOk.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				btnOkMouseClicked(e);
			}
		});
		contentPane.add(btnOk);
		btnOk.setBounds(65, 40, 75, btnOk.getPreferredSize().height);

		contentPane.setPreferredSize(new Dimension(240, 110));
		setSize(240, 110);
		setLocationRelativeTo(getOwner());
		// JFormDesigner - End of component initialization  //GEN-END:initComponents
	}

	// JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
	private JLabel label1;
	public JComboBox cboInterval;
	private JButton btnOk;
	// JFormDesigner - End of variables declaration  //GEN-END:variables
}
